﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using DBSchemaViewer.FrontEnd.DiagramUserControls;

namespace DBSchemaViewer.FrontEnd.Main.Helpers
{
    public class DynamicControlsListHelper
    {        
        /// <summary>
        /// Here we have a hashtalbe by Table internal ID and control
        /// </summary>
        private Dictionary<int, CtlDBTable> _tableControls = new Dictionary<int,CtlDBTable>();

        /// <summary>
        /// Here we have a hashtalbe by Relation internal ID and control
        /// </summary>
        private Dictionary<int, CtlDBRelationship> _RelationshipControls = new Dictionary<int, CtlDBRelationship>();


        public void AddTableControl(int internalID, CtlDBTable tableControl)
        {
            _tableControls.Add(internalID, tableControl);
        }


        public CtlDBTable GetTableControlByID(int internalID)
        {
            return _tableControls[internalID];
        }

        public void removeTableControl(int internalID)
        {
            _tableControls.Remove(internalID);
        }

        public void removeRelationshipControl(int relationInternalID)
        {
            _RelationshipControls.Remove(relationInternalID);
        }

        public int GetTableCount()
        {
            return _tableControls.Count;
        }

        /// <summary>
        /// Add a new relation ship to the list of dynamic controls (relations)
        /// </summary>
        /// <param name="internalID"></param>
        /// <param name="relationshipControl"></param>
        public void AddRelationshipControl(int internalID, CtlDBRelationship relationshipControl)
        {
            _RelationshipControls.Add(internalID, relationshipControl);
        }


        public CtlDBRelationship GetRelationshipControlByID(int internalID)
        {
            return _RelationshipControls[internalID];
        }

        /// <summary>
        /// Just use this when the user wants to create a new document, or load an existing one
        /// </summary>
        public void wipeAllDynamicControls()
        {
            _tableControls.Clear();
            _RelationshipControls.Clear();
            // TODO: Remove relations as well
        }

        public CtlDBTable FullTableAreaHit(List<UIElement> hits)
        {
            CtlDBTable retTable = hits.FirstOrDefault(p => (p as CtlDBTable != null)) as CtlDBTable;

            return retTable;
        }

        /// <summary>
        /// Check in the list of Control Tables if one of them is in the list of hits
        /// (we are going to check only the header)
        /// </summary>
        /// <param name="hits"></param>
        /// <returns></returns>
        public CtlDBTable TableHeaderHit(List<UIElement> hits)
        {
            CtlDBTable retTable = null;

            foreach (CtlDBTable currTableControl in _tableControls.Values)
            {
                if (hits.Contains(currTableControl.HeadingCanvas()))
                {
                    retTable = currTableControl;
                    break;
                }
            }

            return retTable;
        }

    }
}
